
//
//  LJHMineViewController.m
//  HEARME
//
//  Created by liujinhui on 16/1/21.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "LJHMineViewController.h"
#import "LJHLoginViewController.h"
#import "LJHAboutViewController.h"
@interface LJHMineViewController ()

@end

@implementation LJHMineViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    self.view.backgroundColor = [UIColor lightGrayColor];
    
    self.navigationItem.title=@"我的";
    
    [self createTableView];
    
}
#pragma mark - 创建tableView
-(void)createTableView
{
    
    
    
    self.myTableView = [[LJHBaseTableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-40) style:UITableViewStylePlain];
    self.myTableView.contentInset = UIEdgeInsetsMake(WIDTH*0.62 , 0, 0, 0);
    [self.view addSubview:self.myTableView];
    [_myTableView release];
    self.myTableView.dataSource = self;
    self.myTableView.delegate = self;
    [self.myTableView registerClass:[LJHBaseTableViewCell class] forCellReuseIdentifier:@"mineTableCell"];
    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    //图片
    self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, -WIDTH*0.62, WIDTH+10, WIDTH*0.62)];
    self.imageView.image = [UIImage imageNamed:@"mine.jpg"];
    self.imageView.backgroundColor = [UIColor clearColor];
    self.imageView.contentMode = UIViewContentModeScaleAspectFill;
    self.imageView.alpha = 0.7;
    [self.myTableView addSubview:self.imageView];
    [_imageView release];
    
}

//指定多少行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 6;
}



//创建一个cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{    
    NSString *cellIdentifier = @"cell";
    LJHBaseTableViewCell *cell =  [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [[LJHBaseTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        
    }
//    if (indexPath.section==0&&indexPath.row==0) {
//        cell.textLabel.text = @"登录";
//    }
    if (indexPath.section==0&&indexPath.row==0) {
        cell.textLabel.text = @"清除缓存";
    }
    if (indexPath.section==0&&indexPath.row==1) {
        cell.textLabel.text = @"夜间模式";
        
        UISwitch *nightSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(WIDTH*0.8, cell.contentView.bounds.size.height*0.15, WIDTH*0.2, cell.contentView.bounds.size.height*0.8)];
        nightSwitch.backgroundColor = [UIColor clearColor];
        nightSwitch.tintColor = [UIColor grayColor];
        nightSwitch.onTintColor = [UIColor colorWithWhite:0.218 alpha:1.000];
        [cell addSubview:nightSwitch];
        
//        if (nightSwitch.on==YES) {
//            [[NSUserDefaults standardUserDefaults] setObject:@"Mode" forKey:@"night"];
//        }
        
        if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"Mode"] isEqualToString:@"night"])
        {
            [nightSwitch setOn:YES];
        }
        else if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"Mode"] isEqualToString:@"day"])
        {
            
            [nightSwitch setOn:NO];
        }
        [nightSwitch addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
        
        
    }
    
    if (indexPath.section==0&&indexPath.row==2) {
        cell.textLabel.text = @"关于HEARME";
    }
    
    if (indexPath.section==0&&indexPath.row==3) {
        cell.textLabel.text = @"定时";
    }
  
    cell.selectionStyle =  UITableViewCellSelectionStyleNone;
    
   
    return cell;
    
}


#pragma mark - 夜间模式通知
-(void)switchAction:(UISwitch *)switchBtn
{
    
    //夜间
    if (switchBtn.isOn == YES) {
        
        [[NSUserDefaults standardUserDefaults] setObject:@"night" forKey:@"Mode"];
    }
    else
    {
        [[NSUserDefaults standardUserDefaults] setObject:@"day" forKey:@"Mode"];
        
        
    }
    [[NSNotificationCenter defaultCenter] postNotificationName:@"changeColor" object:nil];
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    
    if (indexPath.row == 0) {
        
        //    获得缓存大小
        
        NSInteger size = [[SDImageCache sharedImageCache]getSize];
        
        float sizeMB = size/1024.0/1024.0;
        //            NSLog(@"大小为 ＝ %.2fMB",sizeMB);
        
        
        NSString *str = [NSString stringWithFormat:@"您的缓存数据已达%fMB是否清除",sizeMB];
        UIAlertController *aC = [UIAlertController alertControllerWithTitle:@"提示" message:str preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:aC animated:YES completion:^{
            //            NSLog(@"页面跳转完成");
        }];
        
        
        // 添加一个响应按钮
        UIAlertAction *alertA = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            //清除缓存
            [[SDImageCache sharedImageCache]clearDisk];
        }];
        // 添加提示按钮
        [aC addAction:alertA];
        
        // 创建一个取消按钮
        UIAlertAction *cancelButton = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        }];
        // 添加取消按钮
        [aC addAction:cancelButton];
        }
    if (indexPath.row == 2) {
        LJHAboutViewController *aboutVC = [[LJHAboutViewController alloc]init];
        aboutVC.navigationController.navigationBar.translucent = YES;
        [(LJHTabBarViewController *)self.tabBarController isHiddenTabBar:YES withViewController:aboutVC];
        [self.navigationController pushViewController:aboutVC animated:YES];
    }
    if (indexPath.row == 3) {
        UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"定时关闭" message:nil preferredStyle:UIAlertControllerStyleAlert];
        
        [self presentViewController:alertC animated:YES completion:^{
            
        }];
        
        UIAlertAction *singleCicle = [UIAlertAction actionWithTitle:@"10分钟后关闭" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            [self performSelector:@selector(exitApp) withObject:nil afterDelay:600.0f];

            
        }];
        
        UIAlertAction *queuePlay = [UIAlertAction actionWithTitle:@"30分钟后关闭" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            [self performSelector:@selector(exitApp) withObject:nil afterDelay:1800.0f];
            
        }];
        
        UIAlertAction *randomAction = [UIAlertAction actionWithTitle:@"1小时关闭" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            [self performSelector:@selector(exitApp) withObject:nil afterDelay:3600.0f];
            
        }];
        UIAlertAction *cancelButton = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        }];
        [alertC addAction:cancelButton];
        [alertC addAction:singleCicle];
        [alertC addAction:queuePlay];
        [alertC addAction:randomAction];
        
    }
    
    
}
//设置行高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    //刚开始y的偏移量初始值就是-264
    NSLog(@"y1 === %f",scrollView.contentOffset.y);
    CGFloat y = scrollView.contentOffset.y ;//加上导航栏高度,第一次是-200
    NSLog(@"y2 === %f",y);
    
    if (y < -200) {
        CGRect frame = self.imageView.frame;
        frame.origin.y = y;//偏移了多少,
        frame.size.height =  -y;//tablview向下偏移了多少,高度就是多少
        self.imageView.frame = frame;
    }
    
}

#pragma mark - 关闭app

- (void)exitApp{
    exit(0);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
